package algo.sort;

public class Bubble<T extends Comparable<? super T>> extends Sort<T>{
    @Override
    public void sort(T[] nums) {
        boolean isSorted = false;
        for (int lastIndex=nums.length-1; lastIndex>0 && !isSorted; --lastIndex) {
            isSorted = true;
            for (int i=0; i<lastIndex; ++i) {
                if (greater(nums[i], nums[i+1])) {
                    isSorted = false;
                    swap(nums, i, i+1);
                }
            }
        }
    }

    @Override
    public void sortDesc(T[] nums) {
        boolean isSorted = false;
        for (int lastIndex=nums.length-1; lastIndex>0 && !isSorted; --lastIndex) {
            isSorted = true;
            for (int i=0; i<lastIndex; ++i) {
                if (less(nums[i], nums[i+1])) {
                    isSorted = false;
                    swap(nums, i, i+1);
                }
            }
        }
    }
}
